﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
using Microsoft.AspNet.SignalR;
using WanMeiCommon;
using WanMeiModel;

namespace WanMeiService
{
    public class UserHub : BaseHub
    {
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="model"></param>
        public void Register(UserRegisterModel model)
        {
            string errMsg = string.Empty;
            try
            {
                var rtn = new UserController().Register(model, out errMsg);
                if (rtn == ControllerStatus.success)
                {
                    //成功，构造连接模型，保存当前用户连接
                    var encryptUserName = SecretClass.EncryptQueryString(model.UserName);
                    UserInfoModel currentUser = new UserInfoModel(encryptUserName, 1, Context.ConnectionId,1);
                    PublicDataInfomation.Instance.AddUser(currentUser);
                    base.ReceiveRegisterAndLogin(true, encryptUserName);
                  
                }
                else
                {
                    //失败
                    base.ReceiveRegisterAndLogin(false, errMsg);
                }
            }
            catch (Exception ex)
            {
                LogManger.Instance.WriteLog(ex.ToString(), "UserHub");
                //失败
                base.ReceiveRegisterAndLogin(false, "注册失败");
            }
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="model"></param>
        public void Login(UserLoginModel model)
        {
            string errMsg = string.Empty;
            try
            {
                var rtn = new UserController().Login(model, out errMsg);
                if (rtn == ControllerStatus.success)
                {
                    //成功，构造连接模型，保存当前用户连接
                    var encryptUserName = SecretClass.EncryptQueryString(model.UserName);
                    UserInfoModel currentUser = new UserInfoModel(encryptUserName, 1, Context.ConnectionId,1);
                    PublicDataInfomation.Instance.AddUser(currentUser);
                    base.ReceiveRegisterAndLogin(true, encryptUserName);
                }
                else
                {
                    //失败
                    base.ReceiveRegisterAndLogin(false, errMsg);
                }
            }
            catch (Exception ex)
            {
                //异常
                LogManger.Instance.WriteLog(ex.ToString(), "UserHub");
                base.ReceiveRegisterAndLogin(false, "服务器异常");
            }
        }

        /// <summary>
        /// 处理页面跳转连接，更换connectid，根据userName,重新构造连接模型并且加入连接
        /// </summary>
        /// <param name="userName"></param>
        public void JumpConnect(string userName, string heroId = null)
        {
            //防止断开连接时，移除角色失败
            if (PublicDataInfomation.Instance.GetUserByUserId(userName) == null)
            {
                try
                {
                    //检测username是否合法，存在
                    if (!new UserController().CheckUserIsExist(userName))
                    {
                        base.ReceiveJumpConnect(false, "参数异常");
                        return;
                    }
                    if (!string.IsNullOrEmpty(heroId))
                    {
                        //从选择角色页面进入游戏时，跳转连接事件
                    }
                    else
                    {
                        /*
                         注册，登录=》选择角色
                         游戏=》选择角色
                         */
                        UserInfoModel currentUser = new UserInfoModel(userName, 1, Context.ConnectionId,1);
                        PublicDataInfomation.Instance.AddUser(currentUser);
                        base.ReceiveJumpConnect(true, "");
                    }
                }
                catch (Exception ex)
                {
                    LogManger.Instance.WriteLog(ex.ToString(), "UserHub");
                    base.ReceiveJumpConnect(false, "连接失败");
                }
               
            }
            else
            {
                base.ReceiveJumpConnect(false, "网络异常,请从新登录");
            }

        }

       
    }
}
